<script setup lang="ts">
import { Form, Field, ErrorMessage, configure } from "vee-validate";
// Form: 用于包裹整个表单并提供处理表单的一些行为方法
// Field: 用于包裹单个表单项, 从中我们可以获取到和绑定单个表单项相关的对象
// ErrorMessage: 渲染错误信息

// 验证用户名
function username(value: unknown) {
  if (!value) return "请输入用户名";
  if (typeof value !== "string") return "用户名类型错误";
  if (value.length < 2) return "用户名的长度不能小于2";
  return true;
}

// 验证密码
function password(value: unknown) {
  if (!value) return "请输入密码";
  if (typeof value !== "string") return "密码类型错误";
  if (value.length < 6) return "密码的长度不能小于6";
  return true;
}

// as 指定组件最终渲染成什么标记, Form 组件默认渲染成了 form 标记, 但是我们不需要这个 form 标记, 所以我们指定 as 属性的值为空

configure({
  validateOnBlur: true, // controls if `blur` events should trigger validation with `handleChange` handler
  validateOnChange: true, // controls if `change` events should trigger validation with `handleChange` handler
  validateOnInput: true, // controls if `input` events should trigger validation with `handleChange` handler
  validateOnModelUpdate: true, // controls if `update:modelValue` events should trigger validation with `handleChange` handler
});

// 表单提交事件的处理函数 该函数会在表单验证通过后执行
function onSubmit(values: any) {
  console.log(values);
}
</script>

<template>
  <Form
    :validate-on-mount="true"
    v-slot="{ handleSubmit, handleReset, setFieldValue, setValues }"
    as=""
    :initialValues="{ username: '李四', password: '123456' }"
  >
    <form class="container mt-6" @submit.prevent="handleSubmit(onSubmit)">
      <Field
        name="username"
        class="field"
        as="div"
        v-slot="{ field }"
        :rules="username"
        :validateOnBlur="true"
        :validateOnChange="true"
        :validateOnInput="true"
      >
        <label class="label">用户名</label>
        <div class="control has-icons-left">
          <input
            v-bind="field"
            class="input"
            type="text"
            placeholder="请输入用户名"
          />
          <span class="icon is-small is-left">
            <i class="fas fa-user"></i>
          </span>
        </div>
        <ErrorMessage name="username" v-slot="{ message }">
          <p class="help is-danger">{{ message }}</p>
        </ErrorMessage>
      </Field>
      <Field
        name="password"
        v-slot="{ field }"
        :rules="password"
        class="field"
        as="div"
        :validateOnBlur="true"
        :validateOnChange="true"
        :validateOnInput="true"
      >
        <label class="label">密码</label>
        <div class="control has-icons-left">
          <input
            v-bind="field"
            class="input"
            type="password"
            placeholder="请输入密码"
          />
          <span class="icon is-small is-left">
            <i class="fas fa-key"></i>
          </span>
        </div>
        <ErrorMessage name="password" v-slot="{ message }">
          <p class="help is-danger">{{ message }}</p>
        </ErrorMessage>
      </Field>
      <div class="field is-grouped">
        <div class="control">
          <button type="submit" class="button is-link">提交</button>
        </div>
        <div class="control">
          <button
            @click="handleReset"
            type="button"
            class="button is-link is-light"
          >
            重置
          </button>
        </div>
        <div class="control">
          <button
            @click="setValues({ username: '孙悟空' })"
            type="button"
            class="button is-link is-light"
          >
            设置表单值
          </button>
        </div>
      </div>
    </form>
  </Form>
</template>

<style lang="less" scoped>
@import "https://bulma.io/vendor/fontawesome-free-5.15.2-web/css/all.min.css";
@import "https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css";
</style>